//
//  StatusView.m
//  eye_hunter
//
//  Created by cherry on 10-10-11.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "StateButtonView.h"
#import "HunterConst.h"
#import "PicTarget.h"
#import "ScrollView.h"
#import "MissionView.h"

@implementation StateButtonView
@synthesize posIndex;
@synthesize targets;
@synthesize targetIndex;

- (id)initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])) {
        [self setUserInteractionEnabled:YES];
    }
    return self;
}

-(id)initWithImage:(UIImage *)image{
    if ((self = [super initWithImage:image])) {
        [self setUserInteractionEnabled:YES];
    }
    return self;
}

-(id)init{
    if ((self = [super init])) {
        [self setUserInteractionEnabled:YES];
    }
    return self;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    oldPosision = [[touches anyObject] locationInView:self.superview];
}



-(void) showHint{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:2.0f];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    
    hintView.alpha = 0;
    
    [UIView commitAnimations];
    
    [hintView performSelector:@selector(release) withObject:nil afterDelay:2.0];
}


-(void) hint{
    PicTarget *target =  (PicTarget *)[targets objectAtIndex:targetIndex];
    if (target.isFound) {
        return;
    }
    int x = (target.minX + target.maxX)/2;
    int y = (target.minY + target.maxY)/2;
    int hintIndex = x/384+y/512*2;
    
    CGRect hintFrame = HINT_FRAME[hintIndex];
    hintView = [[UIImageView alloc] initWithFrame:hintFrame];
    hintView.image = [UIImage imageNamed:@"hint.png"];
    MissionView *mainView = (MissionView *)[self.superview superview];
    [mainView addSubview:hintView];
    [mainView addScore:-5];
    [self showHint];
}

-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    ScrollView *parent = (ScrollView *)self.superview;
    CGPoint pt = [[touches anyObject] locationInView:parent];
    CGFloat dist = oldPosision.x - pt.x;
    if (dist > MIN_X_DISTANCE) {
        [parent scrollLeft];
    }else if (dist < (-1) * MIN_X_DISTANCE) {
        [parent scrollRight];
    }else {
        int toCenter = posIndex - BTN_NUM/2;
        if (toCenter == 0) {
            [self hint];
        }else if (toCenter<0) {//右移
            for (int i = 0; i>toCenter; i--) {
                [parent performSelector:@selector(scrollRight) withObject:nil afterDelay:i*(-0.5)];
            }
        }else if (toCenter>0) {//左移
            for (int i = 0; i<toCenter; i++) {
                [parent performSelector:@selector(scrollLeft) withObject:nil afterDelay:i*0.5];
            }
        }
    }

}

-(void) scrollLeft{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.5f];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    self.posIndex -= 1;
    if (self.posIndex < 0) {//出去的情况
        self.posIndex = 6;
        int targetCount = [targets count];
        self.targetIndex += BTN_NUM;
        if (self.targetIndex >= targetCount) {
            self.targetIndex -= targetCount;
        }
        PicTarget *target = (PicTarget *)[targets objectAtIndex:targetIndex];
        if (target.isFound) {
            self.image = [UIImage imageNamed:[target dustPic]];
        }else {
            self.image = [UIImage imageNamed:[target lightPic]];
        }
    }else {
        self.frame = BTN_FRAME[posIndex];
    }
    [UIView commitAnimations];
    self.frame = BTN_FRAME[posIndex];
}

-(void) scrollRight{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.5f];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    self.posIndex += 1;
    if (self.posIndex > 6) {//出去的情况
        self.posIndex = 0;
        int targetCount = [targets count];
        self.targetIndex -= BTN_NUM;
        if (self.targetIndex < 0) {
            self.targetIndex += targetCount;
        }
        PicTarget *target = (PicTarget *)[targets objectAtIndex:targetIndex];
        if (target.isFound) {
            self.image = [UIImage imageNamed:[target dustPic]];
        }else {
            self.image = [UIImage imageNamed:[target lightPic]];
        }
    }else {
        self.frame = BTN_FRAME[posIndex];
    }
    [UIView commitAnimations];
    self.center = BTN_POSIONS[posIndex];
}

-(Boolean) foundTarget: (int) targetId{
    PicTarget *target = (PicTarget *)[targets objectAtIndex:targetIndex];
    if (targetId == target.targetId && !target.isFound) {//找到后的操作
        target.isFound = YES;
        self.image = [UIImage imageNamed:[target dustPic]];
        return YES;
    }
    return NO;
}

- (void)dealloc {
    [super dealloc];
}


@end
